// pages/rankinglist/rankinglist.js
var app = getApp();
var config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      pageIndex: 1,
      animationData1: {},
      animationData2: {},
      animationData3: {},
      currSelectedIndex: 1,
      timeType:1,
      nodata:false,
      nodatas:false,
      indexs:1,
      logoSrc:'../../image/home_ranklist_last_icon_1@2x.png'
  },

 lower: function () {
    var that=this;
   if (that.data.pageIndex<5){
    that.setData({
      loading: false,  //把"上拉加载"的变量设为false，显示 
      pageIndex: that.data.pageIndex+1
    })

    // 上拉获取更多数据
    this.getToplist() 
    }
  },



  toshow: function () {
    // wx.navigateTo({
    //   url: '../toplistshow/toplistshow',
    // })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userFaceUrl: app.globalData.userInfo.avatarUrl,
      nickName: app.globalData.userInfo.nickName,
      pageIndex: 1
    });
    this.getToplist();
    this.getmyToplist();
  },
 

  /**
  *获取排行榜列表
  */
  getToplist: function (){
    var that = this;
    wx.request({
      url: config.topList,
      method:"POST",
      data: {
        type:1 ,//冲榜达人
        pageSize:10,
        timeType:that.data.timeType,
        page:that.data.pageIndex
      },
 
      success: function (res) {
        if (res.data.success == 0) {
          if (that.data.pageIndex ==1){
            if (res.data.rows.length==0){
              that.setData({
                nodata: true,
              });
              console.log(that.data.nodata)
            }else{
              that.setData({
                nodata: false,
              });
            }
            that.setData({
              toplist: res.data.rows,
            });
          }else {
            //加载更多
            var list = that.data.toplist;
            if (res.data.rows.length > 0){
              for (var i = 0; i < res.data.rows.length; i++) {
                list.push(res.data.rows[i]);
              }
              that.setData({
                toplist: list
              });
            }
          }
        }
      }
    });

  },

  /**
   *点击切换
   */
  onTabsItemTap: function (event) {
    var that=this;
    this.setDefaultAnim();

    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'linear',
    })
    this.animation = animation
    animation.scale(1.2, 1.2).step()
    var index =parseInt(event.currentTarget.dataset.index);
    if(index ==1){
      this.setData({
        animationData1: animation.export(),
        currSelectedIndex: 1,
        indexs:1,
        logoSrc:'../../image/home_ranklist_last_icon_1@2x.png'
      });
      this.getmyToplist();
    }else if(index ==2){
      this.setData({
        animationData2: animation.export(),
        currSelectedIndex: 2,
        indexs:3,
        logoSrc:'../../image/home_ranklist_this_icon_1@2x.png'
      });
      this.getmyToplist();
    }else{
      this.setData({
        animationData3: animation.export(),
        currSelectedIndex: 3,
        indexs: 2,
        logoSrc:'../../image/home_ranklist_icon_1@2x.png'
      }); 
      this.getmyToplist();
    }

    this.setData({
      timeType: that.data.indexs,
      pageIndex:1
    })
   this.getToplist(); 
  },

  setDefaultAnim: function(){
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'linear',
    })
    this.animation = animation
    animation.scale(1, 1).step()
    if (this.data.currSelectedIndex == 1){
      this.setData({
        animationData1: animation.export()
       
      })
    } else if (this.data.currSelectedIndex == 2){
      this.setData({
        animationData2: animation.export()

      })
    }else{
      this.setData({
        animationData3: animation.export()

      })
    }
   

  },



  /**
  *获取我的排行榜
  */
  getmyToplist: function () {
    var that = this;
    console.log(that.data.indexs)
    var openId = app.globalData.openid;
    wx.request({
      url: config.myTopList,
      method: "POST",
      data: {
        type: 1,
        timeType:that.data.indexs,
        openId: app.globalData.openid,
      },
      success: function (res) {
        console.log(config.topList)
        if (res.data.success == 0) {
          console.log(res.data.rows.length);
          
          if (res.data.rows[0].rownum==null){
            that.setData({
              nodatas:true
            })
          }else{
            console.log(5555)
            that.setData({
              nodatas: false
            })
          }
          that.setData({
            myTopList: res.data.rows,
          });
        }
      }
    });

  },


  /**
   * 分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '我正在挑战法律常识，一起来冲榜吧！',
      path: '/pages/index/index?pageId=' + config.toplist
    }
  }
})